﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Windows.Forms;
using Ycjw.PipeLine.Commons.SerializeHelper;
using Ycjw.PipeLine.Map.Basic.Conditions;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    public class FlowDirectorToolBar : BasicToolBar
    {
        private Layer3D m_ThemeLayer;
        private Dataset m_Dataset;
        private static readonly string THEME_LAYER_NAME = "流向";

        public FlowDirectorToolBar() : base()
        {
            var showToolStripButton = new ToolStripButton()
            {
                DisplayStyle = ToolStripItemDisplayStyle.ImageAndText,
                Text = "显示选中管线流向",
                Name = "showDirector"
            };
            base.ToolStrip.Items.Add(showToolStripButton);

            var hideToolStripButton = new ToolStripButton()
            {
                DisplayStyle = ToolStripItemDisplayStyle.ImageAndText,
                Text = "关闭流向",
                Name = "hideDirector"
            };
            base.ToolStrip.Items.Add(hideToolStripButton);

            showToolStripButton.Click += ShowToolStripButton_Click;
            hideToolStripButton.Click += HideToolStripButton_Click;

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.SceneControl.ObjectSelected -= SceneControl_ObjectSelected;
            this.SceneControl.ObjectSelected += SceneControl_ObjectSelected;
        }

        private void HideToolStripButton_Click(object sender, EventArgs e)
        {
            RemoveThemeLayer();
        }
        private void RemoveThemeLayer()
        {
            if (m_ThemeLayer == null)
            {
                return;
            }

            if (Scene.Layers.Contains(THEME_LAYER_NAME))
            {
                Scene.Layers.Remove(THEME_LAYER_NAME);
            }
        }
        private void ShowToolStripButton_Click(object sender, EventArgs e)
        {
            RemoveThemeLayer();
            if (m_Dataset == null)
            {
                Sunny.UI.UIMessageTip.ShowWarning("请选择一条管线");
                return;
            }
            Theme3DCustom theme3d = new Theme3DCustom
            {
                LineSymbolIDExpression = IniHelper.DirectionSymbleID
            };
            m_ThemeLayer = SceneControl.Scene.Layers.Add(m_Dataset, theme3d, true, THEME_LAYER_NAME);
            m_ThemeLayer.Caption = THEME_LAYER_NAME;
            ((SuperMap.Realspace.Layer3DSettingVector)((SuperMap.Realspace.Layer3DDataset)m_ThemeLayer).AdditionalSetting).Style.AltitudeMode = AltitudeMode.RelativeToUnderground;

        }
        private void SceneControl_ObjectSelected(object sender, SuperMap.UI.ObjectSelectedEventArgs e)
        {
            if (e.Count <= 0)
            {
                return;
            }
            Selection3D[] selection3d = SceneControl.Scene.FindSelection(true);
            if (selection3d.Length == 0) return;
            var selectFeature = selection3d[0].ToRecordset();
            if (selectFeature.GetGeometry().Type != GeometryType.GeoLine3D)
            {
                Sunny.UI.UIMessageTip.Show("请选择一条管线");
                return;
            }

            var layer = selection3d[0].Layer;
            Layer3DDataset layer3DDataset = layer as Layer3DDataset;
            m_Dataset = layer3DDataset.Dataset;

        }
        public override void AfterClose()
        {
            this.SceneControl.ObjectSelected -= SceneControl_ObjectSelected;
            this.RemoveThemeLayer();
        }
    }
}
